package com.example.fufudamowang.gridview;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.app.Activity;

import static android.R.attr.left;
import static com.example.fufudamowang.gridview.R.id.bottom;
import static com.example.fufudamowang.gridview.R.id.top;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        setTitle("网格师徒");
        GridView gv=(GridView)findViewById(R.id.gv);
        gv.setAdapter(new ImageAdapter(this));
    }



    public class ImageAdapter extends BaseAdapter{
        private Context mContext;

        public ImageAdapter(Context c){
            mContext=c;
        }

        private Integer[] mThumbIds={
                R.drawable.p1,
                R.drawable.p2,
                R.drawable.p3,
                R.drawable.p4,
                R.drawable.p5,
                R.drawable.p6
        };

        public int getCount(){//获取图片个数
            return mThumbIds.length;
        }
        public Object getItem(int arg0){//获取图片在图库中的位置
            return null;
        }
        public long getItemId(int arg0){//获取图片在图库中位置
            return 0;
        }



        public View getView(int position, View convertView, ViewGroup parent){
            ImageView imageView;
            if (convertView==null){
                imageView=new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85,85));//设置ImageView对象布局
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8,8,8,8);//设置间距
            }
            else{
                imageView=(ImageView)convertView;
            }
            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

    }

}
